const {readFileSync, writeFileSync} = require('fs')
const {resolve, join} = require('path')
const Results = require('../../config/results')
let p = join(__dirname, '../../', '/config', '/setting.json')
module.exports = {
    getSetting(req, res, next) {
        let json = readFileSync(p, {encoding: 'utf8'})
        res.status(200).json(Results.success(200, 'setting', JSON.parse(json)))
    },
    setSetting(req, res, next) {
        //到这里接收到的就是一个json字符串
        try {
            let json = req.body.json
            json = Object.prototype.toString.call(json) !== '[object String]' ? JSON.stringify(json) : json
            writeFileSync(p, json, {encoding: 'utf8'})
        } catch (e) {
            return res.status(500).json(Results.fail(500, e.message||'保存配置文件发生错误！', e))
        }

        res.status(200).json(Results.success(200, 'setting', [{message: '修改成功!'}]))

    }
}